package com.zycoin.common.plugin.dubbo;

import com.alibaba.dubbo.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(DubboProperties.class)
public class DubboAutoConfiguration {

    @Autowired
    private DubboProperties dubboProperties;

    @Bean
    @ConditionalOnMissingBean(ApplicationConfig.class)
    @ConditionalOnProperty(value="name",prefix = "spring.dubbo.application")
    public ApplicationConfig requestApplicationConfig() {
        return dubboProperties.getApplication();
    }

    @Bean
    @ConditionalOnMissingBean(RegistryConfig.class)
    @ConditionalOnProperty(value="address",prefix = "spring.dubbo.registry")
    public RegistryConfig requestRegistryConfig() {
        return dubboProperties.getRegistry();
    }

    @Bean
    @ConditionalOnMissingBean(ProtocolConfig.class)
    @ConditionalOnProperty(value="name",prefix = "spring.dubbo.protocol")
    public ProtocolConfig requestProtocolConfig() {
        return dubboProperties.getProtocol();
    }

    @Bean
    @ConditionalOnMissingBean(MonitorConfig.class)
    @ConditionalOnProperty(value="",prefix = "spring.dubbo.monitor")
    public MonitorConfig requestMonitorConfig() {
        return dubboProperties.getMonitor();
    }
    
    @Bean
    @ConditionalOnMissingBean(ModuleConfig.class)
    @ConditionalOnProperty(value="name",prefix = "spring.dubbo.module")
    public ModuleConfig requestModuleConfig(){
    	return dubboProperties.getModule();
    }
    
    @Bean
    @ConditionalOnMissingBean(MethodConfig.class)
    @ConditionalOnProperty(value="name",prefix = "spring.dubbo.method")
    public MethodConfig requestMethodConfig(){
    	return dubboProperties.getMethod();
    }
//
//    @Bean
//    @ConditionalOnMissingBean(ProviderConfig.class)
//    @ConditionalOnProperty(name="name",prefix = "spring.dubbo.provider")
//    public ProviderConfig requestProviderConfig(){
//        ProviderConfig providerConfig = dubboProperties.getProvider();
//        if(providerConfig == null){
//            providerConfig = new ProviderConfig();
//        }
//        return providerConfig;
//    }
//
//    @Bean
//    @ConditionalOnMissingBean(ConsumerConfig.class)
//    @ConditionalOnProperty(value="",prefix = "spring.dubbo.consumer")
//    public ConsumerConfig requestConsumerConfig(){
//    	ConsumerConfig consumerConfig = dubboProperties.getConsumer();
//    	if(consumerConfig == null){
//    		consumerConfig = new ConsumerConfig();
//    	}
//    	return consumerConfig;
//    }

}
